﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Forms;
using Facebook.Entity;
//using Facebook.facebook.schemas;
using FacebookDesktopSample.Properties;

namespace FacebookDesktopSample
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void OnLoad()
		{
			try
			{
				var FacebookAPI = new Facebook.API.FacebookAPI
				                  	{
				                  		IsDesktopApplication = true,
				                  		ApplicationKey = Settings.Default.api_key,
				                  		Secret = Settings.Default.secret
				                  	};
				FacebookAPI.ConnectToFacebook();

				LoadUser(FacebookAPI.GetUserInfo());
				LoadFriends(FacebookAPI.GetFriends());
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
				Close();
			}
		}

		private void LoadUser(User user)
		{
			label1.Text = user.FirstName;

			if (user.PictureUrl != null)
			{
				pbProfilePicture.ImageLocation = user.PictureUrl.ToString();
			}
			else
			{
				pbProfilePicture.Image = user.Picture;
			}
		}

		private void LoadFriends(ICollection<User> friends)
		{
			foreach (User friend in friends)
			{
				//nothing
			}

			var plural = friends.Count != 1 ? "s" : string.Empty;
			lblFriendCount.Text = String.Format(CultureInfo.InvariantCulture, Resources.lblFriendCount, friends.Count, plural);
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			OnLoad();
		}


	}
}
